import {mapGetters} from 'vuex';
import globalHelper from "@/utils/globalHelper";

export default {
    data() {
        return {
            filters: {
                username: ''
            },
            checkRoles: [],
            addForm: {},
            addFormVisible: false,
            editForm: {},
            editFormVisible: false,
            restPwdForm: {},
            restPwdFormVisible: false,
            addFormRules: {
                username: [
                    {required: true, message: "请输入用户名", trigger: "blur"}
                ],
                password: [
                    {required: true, message: "请输入密码", trigger: "blur"}
                ],
                admin: [
                    {required: true, message: '请选择是否是管理员', trigger: 'change'}
                ],

            },
            editFormRules: {
                admin: [
                    {required: true, message: '请选择是否是管理员', trigger: 'change'}
                ],
            },
            restPwdFormRules: {
                password: [
                    {required: true, message: '请输入新密码', trigger: 'blur'}
                ],
            },
        }
    },
    computed: {
        ...mapGetters([
            'managerList',
            'managerPage'
        ])
    },
    watch: {},
    components: {},
    mounted() {
        this.getAllDataOpt();
    },
    methods: {
        getAllDataOpt: function () {
            this.$store.dispatch('getManagerList', {...this.filters});
        },

        handleCurrentChange(val) {
            this.managerPage.page = val - 1;
            this.getAllDataOpt();
        },
        handleSizeChange(val) {
            this.managerPage.page = 0;
            this.managerPage.rows = val;
            this.getAllDataOpt();
        },
        handleSearch() {
            this.managerPage.page = 0;
            this.getAllDataOpt();
        },
        tableOperate(typeOpt, row) {

            if (typeOpt === 'edit') {
                this.editForm = Object.assign({}, row);
                this.checkRoles = row.roles;
                this.editFormVisible = true;
            } else if (typeOpt === 'add') {
                this.addForm = {};
                this.checkRoles = []
                this.addFormVisible = true;
            } else if (typeOpt === 'rest') {
                this.restPwdForm = {
                    id: row.id
                };
                this.restPwdFormVisible = true;
            } else if (typeOpt === 'del') {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$store.dispatch('deleteManager', {id: row.id}).then((res) => {
                        this.getAllDataOpt();
                    });
                });
            }
        },
        addFormSubmit: function () {
            globalHelper.formValidate(this, 'addForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.addForm);
                    requestData.roles = this.checkRoles;
                    this.$store.dispatch('addManager', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '创建管理员成功'
                        });
                        this.getAllDataOpt();
                        this.addFormVisible = false;
                    });
                }
            });

        },
        editFormSubmit: function () {
            globalHelper.formValidate(this, 'editForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.editForm);
                    requestData.roles = this.checkRoles;
                    this.$store.dispatch('editManager', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '修改管理员成功'
                        });
                        this.getAllDataOpt();
                        this.editFormVisible = false;
                    });
                }
            });
        },
        restPwdFormSubmit: function () {
            globalHelper.formValidate(this, 'restPwdForm').then((valid) => {
                if (valid) {
                    let requestData = Object.assign({}, this.restPwdForm);
                    this.$store.dispatch('resetPasswordManager', requestData).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '重置密码成功'
                        });
                        this.restPwdFormVisible = false;
                    });
                }
            });


        },

    }
}
